import { _decorator, Component, Node, Prefab, instantiate, Button,tween ,Vec3,input,Input,Animation,animation, EventMouse} from 'cc';
import LayoutEx from './components/LayoutEx';
import {HomeView} from "./HomeView";
const { ccclass, property } = _decorator;

@ccclass('MainHome')
export class MainHome extends Component {
    @property({type:Node,tooltip:"背景"})
    bg:Node = null;

    @property({type:Prefab,tooltip:'首页预制体'})
    HomePage:Prefab = null;

    @property({type:Node,tooltip:'预制体容器'})
    MainHomeBody:Node = null;

    @property({type:Node,tooltip:"home组建"}) 
    tween:Node = null;

    @property({type:Node,tooltip:"按钮组"}) 
    btnGroup:Node = null;

    @property({type:Node,tooltip:"小人"}) 
    xiaoRen:Node = null;

    @property(Animation)
    private animation: Animation | null = null;

    @property(Animation)
    BodyAnim:Animation = null;

    private isZhanKai = false;

    static(){
       
    }

    onLoad() {
        if (this.bg) {
            LayoutEx.scaling(this.bg);
        }
         // const state = this.BodyAnim.getState('oneStep');
        if(this.xiaoRen){
            this.xiaoRen.on(Button.EventType.CLICK,()=>{
                this.xiaoRen.getComponent(Animation).play();
            },this)
        }
        let HomePageEle = instantiate(this.HomePage);
        this.MainHomeBody.addChild(HomePageEle)
        const childComp = HomePageEle.getComponent(HomeView);
        childComp.dataInfo = {name:"boonook",status:"1"};
        // 注入父组件引用，便于子预制体直接调用父方法
        // @ts-ignore
        childComp.parentMainHome = this;
        childComp.updateData();

        if(this.tween){
            this.tween.on(Button.EventType.CLICK,()=>{
                this.isZhanKai = !this.isZhanKai;
                if(this.isZhanKai){
                    tween(this.tween)
                    .to(0.2, { eulerAngles: new Vec3(0, 0, -200) })  // 稍微超过180度
                    .to(0.1, { eulerAngles: new Vec3(0, 0, -170) })   // 回弹一点
                    .to(0.1, { eulerAngles: new Vec3(0, 0, -180) })   // 最终停在180度
                    .call(() => {

                    })
                    .start();
                }else{
                   tween(this.tween)
                    .to(0.2, { eulerAngles: new Vec3(0, 0, 20) })  // 稍微超过180度
                    .to(0.1, { eulerAngles: new Vec3(0, 0, -10) })   // 回弹一点
                    .to(0.1, { eulerAngles: new Vec3(0, 0, 0) })   // 最终停在180度
                    .call(() => {
                        
                    })
                    .start(); 
                }
            },this)
        }
        if(this.btnGroup){
            this.btnGroup.children[0].on(Button.EventType.CLICK,()=>{
                console.log('点击了按钮1');
            },this)
            this.btnGroup.children[1].on(Button.EventType.CLICK,()=>{
                console.log('点击了按钮2');
            },this)
            this.btnGroup.children[2].on(Button.EventType.CLICK,()=>{
                console.log('点击了按钮3');
            },this)
        }
    }

    start() {
        // 监听添加了预制体的容器节点，因为子节点通过 this.node.parent?.emit 在此节点上派发事件
        this.MainHomeBody?.on('listUpdate', (data)=>{
            ////事件通信（低耦合，推荐复杂场景）
            console.log('listUpdate----------------:', data);
        }, this)
    }

    // 供子预制体调用的方法
    public parentMethod(msg: string) {
        console.log("父预制体方法被调用，收到消息：", msg);
        // 父预制体的逻辑处理...
    }

    protected onEnable(): void {
        
    }

    update(deltaTime: number) {
        ////deltaTime：上一帧到当前帧的时间间隔（秒）
        
    }

    registerChild(target){
        console.log('registerChild--------------:')
    }

    onDestroy() {
        // 解绑与本组件相关的监听
        this.MainHomeBody?.targetOff(this);
        // 移除事件监听，避免内存泄漏
    }
}

